package com.publish.file.controller;

public class Result<Response> {

	public static final int CODE_SUCCESS = 200;
	public static final String CODE_SUCCESS_STRING = "成功!";
	public static final int CODE_NOPERMIT = 401;
	public static final String CODE_NOPERMIT_STRING = "验证不通过!";
	public static final int CODE_ERROR = 500;
	public static final String CODE_ERROR_STRING = "服务异常!";
	public static final int CODE_AUTH_DENIED = 400;
	public static final String CODE_AUTH_DENIED_STRING = "没权限!";
	public static final int CODE_GEN_TOKEN = 502;
	public static final String CODE_GEN_TOKEN_STRING = "服务异常!";
	public static final int CODE_QUERY_ERROR = 600;
	public static final String CODE_QUERY_ERROR_STRING = "数据库异常!";
	public static final int CODE_SQL_ERROR = 601;
	public static final String CODE_SQL_ERROR_STRING = "数据库异常!";
	public static final int CODE_REQUEST = 300;
	public static final String CODE_REQUEST_STRING = "请求错误!";
	private int code = CODE_SUCCESS;
	private String message;
	private Response response;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}

	static class Success<T> extends Result<T> {
		public Success(T t) {
			this(t, "成功!");
		}

		public Success(T t, String message) {
			setResponse(t);
			setCode(Result.CODE_SUCCESS);
			setMessage(message);
		}
	}

	static class Fail<T> extends Result<T> {
		public Fail(int code, String message) {
			setCode(code);
			setMessage(message);
		}
	}

	public static <T> Result<T> success(T t) {
		return new Success<T>(t);
	}

	public static <T> Result<T> success(T t, String message) {
		return new Success<T>(t, message);
	}

	public static <T> Result<T> fail(int code, String message) {
		return new Fail<T>(code, message);
	}
}
